const { redisClient, redisPrefix } = require("../config");

const dataScopeKey = 'user_data_scope_1'
async function getUserDataFromRedis(user_id) {
  const key = redisPrefix + user_id;
  const data = await redisClient.get(key);
  if (data) {
    // console.log("Fetching user data from redis for key:", key);
    let userInfo = JSON.parse(data)
    return userInfo;
  }
  return null;
}

function setUserDataToRedis(user_id, data) {
  const key = redisPrefix + user_id;
  if (hasRedisKey(key)) {
    deleteUserDataToRedis(key);
  }
  // console.log("Setting user data to redis for key:", key, data);
  redisClient.set(key, JSON.stringify(data));
}

function deleteUserDataToRedis(user_id) {
  const key = redisPrefix + user_id;
  // console.log("delete user data to redis for key:", key);
  redisClient.del(key);
}

function hasRedisKey(user_id) {
  const key = redisPrefix + user_id;
  return redisClient.exists(key);
}

function setDataScopeToRedis(user_id,dataScope) {
  let key = dataScopeKey + user_id
  if (hasRedisKey(key)) {
    deleteUserDataToRedis(key);
  }
  // console.log("Setting user datascope to redis for key:", key, data);
  redisClient.set(key, JSON.stringify(dataScope));
}

async function getUserDataScopeFromRedis(user_id) {
  const key = dataScopeKey + user_id;
  const data = await redisClient.get(key);
  if (data) {
    // console.log("Fetching user datascope from redis for key:", key);
    return data;
  }
  return null;
}
module.exports = {
  getUserDataFromRedis,
  setUserDataToRedis,
  deleteUserDataToRedis,
  hasRedisKey,
  setDataScopeToRedis,
  getUserDataScopeFromRedis,
};
